package com.zjsru.plan2024.oneday;

import java.util.Arrays;

/**
 * 3194. 最小元素和最大元素的最小平均值
 *
 * @Author: cookLee
 * @Date: 2024-10-16
 */
public class MinimumAverage {

    /**
     * 主
     * \
     * 输入： nums = [7,8,3,4,15,13,4,1]
     * 输出： 5.5
     * \
     * 输入： nums = [1,9,8,3,10,5]
     * 输出： 5.5
     * \
     *
     * @param args args
     */
    public static void main(String[] args) {
        MinimumAverage minimumAverage = new MinimumAverage();
        int[] nums = new int[]{7, 8, 3, 4, 15, 13, 4, 1};
        System.out.println(minimumAverage.minimumAverage(nums));
    }

    /**
     * 最小平均值:双指针
     *
     * @param nums nums
     * @return double
     */
    public double minimumAverage(int[] nums) {
        Arrays.sort(nums);
        int len = nums.length;
        double ans = Double.MAX_VALUE;
        for (int i = 0, j = len - 1; i <= j; ) {
            ans = Math.min(ans, (nums[i] + nums[j]) / 2.0);
            i++;
            j--;
        }
        return ans;
    }

}
